/*
@Time : 2020-01-13 10:37
@Author : wj
@File : requestid
@Software: GoLand
*/

package middleware

import (
	"github.com/gin-gonic/gin"
	"github.com/satori/go.uuid"
)

func RequestId() gin.HandlerFunc {
	return func(c *gin.Context) {
		requestId := c.Request.Header.Get("X-Request-Id")

		//生成一个32位的UUID
		if requestId == "" {
			u4 := uuid.NewV4()
			requestId = u4.String()
		}

		// X-Request-Id重新赋值
		c.Set("X-Request-Id", requestId)
		c.Writer.Header().Set("X-Request-Id", requestId)

		c.Next()

	}
}
